package com.chenggong.log

import androidx.appcompat.app.AppCompatActivity
import com.chenggong.log.dialog.ViewLogDialog
import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by chenggong on 18-5-2023.
 * 视图日志
 */
class ViewLog(private val activity: AppCompatActivity) {
    private var viewLogDialog: ViewLogDialog = ViewLogDialog()

    fun show(){
        viewLogDialog.show(activity.supportFragmentManager,"view_log_dialog")
    }

    fun i(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.I
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    fun e(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.E
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    fun v(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.V
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    fun d(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.D
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    fun w(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.W
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    fun wtf(tag: String, message: String) {
        val logRecord = LogRecord().apply {
            this.tag = tag
            this.msg = message
            this.level = LogLevel.WTF
            this.time = getTimeString()
        }
        viewLogDialog.updateData(logRecord)
    }

    private fun getTimeString(): String{
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
        val date = Date()
        return format.format(date)
    }
}